<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>岗位授权</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<body class="iframeBody">
<div class="Wrapper">
    <table class="layout2col1">
        <tbody>
            <tr>
                <td style="width: 280px;" valign="top">
                    <div style="width: 280px;" class="sqPanel">
                        <div class="sqPanel_head">
                            <span><i class="sqIco sqIco_3"></i>组织架构</span>
                        </div>
                        <div class="sqPanel_body" style="min-height: 450px;">
                            <div class="btnsBox">&nbsp;</div>
                            <div class="nanoWrap" style="height: 450px;">
                                <div class="nano"><div class="nano-content">
                                    <ul id="tree1">
                                    </ul>
                                </div></div>
                            </div>
                        </div>
                    </div>
                </td>
                <td style="width: 270px;" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar1" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">角色</div>
                                        <div class="field">
                                             <input name="roleName" id="roleName" type="text" class="ui-textbox" value="">
                                        </div>
                                    </div>
                                    <span id="searchBtn1" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body" style="min-height: 400px;">
                            <div class="FBL5">
                                <span id="addRole_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_3"></i>新增</span>
                                <span id="deleteRole_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_17"></i>移除</span>
                            </div>
                            <div id="ligergrid_roles" class="sq-grid" style="height: 450px;"> </div>
                        </div>
                    </div>
                </td>
                <td width="300" valign="top">
                    <div class="sqPanel">
                        <div class="sqPanel_head">
                            <form id="searchBar2" class="titSearchBar">
                                <div class="fieldset1 clearFix">
                                    <div class="fieldBox">
                                        <div class="label">已添加用户</div>
                                        <div class="field">
                                             <input name="usrNm" id="usrNm" type="text" class="ui-textbox" value="">
                                        </div>
                                    </div>
                                    <span id="searchBtn2" class="sq-search-button"></i>查询</span>
                                </div>
                            </form>
                        </div>
                        <div class="sqPanel_body">
                            <div class="FBL5">
                                <span id="addPE_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_3"></i>新增</span>
                                <span id="deletePE_btn" class="sq-button sq-button-hasicon"><i class="sqBtnIco_17"></i>移除</span>
                            </div>
                            <div id="ligergrid_role_pe" class="sq-grid"></div>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>

    <div id="addRoles_Html" style="display:none">
        <form id="searchBar_addRoles" class="l-form">
            <div class="fieldset1 clearFix">
                <div class="FBL5" style="padding: 0;">
                    <div class="fieldBox">
                        <div class="label">角色</div>
                        <div class="field">
                             <input name="roleName2" id="roleName2" type="text" class="ui-textbox" value="" ligerui="width: 100">
                        </div>
                    </div>
                    <span id="searchBtn_addRoles" class="sq-button sq-button-hasicon"><i class="sqBtnIco_1"></i>查询</span>
                </div>
            </div>
        </form>
        <div id="ligergrid_addRoles" class="sq-grid"></div>
        <div class="btnsBox TC">
            <span class="sq-button sq-button-hasicon l-dialog-btn-ok"><i class="sqBtnIco_30"></i>确认</span>
            <span class="sq-button sq-button-hasicon l-dialog-btn-cancel"><i class="sqBtnIco_14"></i>取消</span>
        </div>
    </div>
    <div id="addPE_Html" style="display:none">
        <form id="searchBar_addPE" class="l-form">
            <div class="fieldset1 clearFix">
                <div class="FBL5" style="padding: 0;">
                    <div class="fieldBox">
                        <div class="label">用户名</div>
                        <div class="field">
                             <input name="usrNmSearch" id="usrNmSearch" type="text" class="ui-textbox" value="" ligerui="width: 100">
                        </div>
                    </div>
                    <span id="searchBtn_addPE" class="sq-button sq-button-hasicon"><i class="sqBtnIco_1"></i>查询</span>
                </div>
            </div>
        </form>
        <div id="ligergrid_addPE" class="sq-grid"></div>
        <div class="btnsBox TC">
            <span class="sq-button sq-button-hasicon l-dialog-btn-ok"><i class="sqBtnIco_30"></i>确认</span>
            <span class="sq-button sq-button-hasicon l-dialog-btn-cancel"><i class="sqBtnIco_14"></i>取消</span>
        </div>
    </div>



<script type="text/javascript">

var tree,
    grid_roles,
    grid_role_pe,
    grid_addRoles,
    grid_addPE;
$(function () {
    tree = $("#tree1").ligerTree({
    	url: '<%=path%>/system/sysOrg/initList.do',
        ajaxType: 'get',
        checkbox: false,
        // single: true,
        parentIcon: "SqFolder",
        childIcon: "SqLeaf",
        needCancel:false,
        isExpand: false,
        isLeaf: function (data) {
           /*  if (!data) return false;
            if (data.hasChildren) {return false} else {return true}; */
        	if (!data) return false;
            if (data.hasChildren) {return false} else if(data.children){return false} else {return true};
        },
        delay: function(e)
        {
           <%--  var data = e.data;
            if (data.hasChildren)
            {
                return { url: '<%=path%>/system/sysOrg/treeList.do?id=' + data.id }
            }
            return false; --%>
        },
        onAfterAppend: function (parentNode, newdata) {
            setNanoScroller();
        },
        onExpand: function (node) {
            setNanoScroller();
        },
        onCollapse: function (node) {
            setNanoScroller();
        },
        onSelect: function (node) {
            //alert(JSON.stringify(node));
            set_ligergrid_roles (node.data.id);
        },
        onCancelselect: function (data,target) {
            // debugger;
            grid_roles.grid.hide();
            if (grid_role_pe) {grid_role_pe.grid.hide()}
        },
        onSuccess: function (data) {
            setNanoScroller();
            tree.selectNode(data[0]);
        }
    });
    $("#searchBar1").ligerForm();
    $("#searchBtn1").click(function(event) {
        if (!tree.getSelected()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        var form=liger.get("searchBar1");
        var data = form.getData();
        data.structureID=tree.getSelected().data.id;
       // alert(JSON.stringify(data));
        for (name in data) {grid_roles.setParm(name, data[name])};
        grid_roles.reload();
    });
    $("#addRole_btn").click(function(event) {
        if (!tree.getSelected()) {
        	top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        $.ligerDialog.SQopen({
            width: 600,
            top: 25,
            title : '新增角色',
            target: $("#addRoles_Html")
        });
        $("#searchBar_addRoles").ligerForm();
        clearLigerForm("searchBar_addRoles");
        var parms={
            "orgId": tree.getSelected().data.id,
            "roleName2"  : ""
        };
        if (grid_addRoles) {
            for (name in parms) {grid_addRoles.setParm(name, parms[name])};
            grid_addRoles.reload();
        }else{
            render_ligergrid_addRoles (parms);
        };
    });
    $("#searchBtn_addRoles").click(function(event) {
        var form=liger.get("searchBar_addRoles");
        var data = form.getData();
        data.structureID=tree.getSelected().data.id;
       // alert(JSON.stringify(data));
        for (name in data) {grid_addRoles.setParm(name, data[name])};
        grid_addRoles.reload();
    });
    $("#deleteRole_btn").click(function(event) {
        if (!tree.getSelected()) {
        	top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        var selectedRoles=grid_roles.getSelecteds();
        if (selectedRoles.length==0) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        var roleIdArr=[];
        var orgId=tree.getSelected().data.id;
        for (var i = 0; i < selectedRoles.length; i++) {
        	roleIdArr.push(selectedRoles[i].id);
        };
        //alert(orgId+"--------------"+JSON.stringify(roleIdArr));
        $.ligerDialog.confirm('确认移除？如果移除角色将角色下的用户也将移除!确认点击是', function (flag) {
			if(flag == false)return;
	        $.ajax({
	  			type: 'post',
	  			dataType:'json',
	  			data:{orgId:orgId,roleIdArr:roleIdArr},
	  			url: '<%=path%>/system/sysUsrFunAuthR/removeRoleByOrgId.do',
	  			success:function(msg){
	  				 if(msg.success == 1){
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
	  					grid_roles.reload();
	  				 }else{
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
	  				 }
	  			}
	  		});
        });
        
    });
    $("#addRoles_Html .l-dialog-btn-ok").click(function() {
        var selectedRoles=grid_addRoles.getSelecteds();
        if (selectedRoles.length==0) {
        	top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        var roleIdArr=[];
        var orgId=tree.getSelected().data.id;
        for (var i = 0; i < selectedRoles.length; i++) {
        	roleIdArr.push(selectedRoles[i].id);
        };
        //alert(orgId+"----"+JSON.stringify(roleIdArr));
        $.ajax({
  			type: 'post',
  			dataType:'json',
  			data:{orgId:orgId,roleIdArr:roleIdArr},
  			url: '<%=path%>/system/sysUsrFunAuthR/addOrgRole.do',
  			success:function(msg){
  				 if(msg.success == 1){
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
  					grid_roles.reload();
  				 }else{
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
  				 }
  			}
  		});
        $.ligerDialog.hide();
    });
    $("#searchBar2").ligerForm();
    $("#searchBtn2").click(function(event) {
        if (!tree.getSelected()) {
            ligerDialogWarn(LogUtils.LOG_SYS_ORG_SELECT_ORG, 1000);
            return;
        };
        var selectedRoles=grid_roles.getSelecteds();
        if (selectedRoles.length>1) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE_ONLY,SQ_type: "warning"});
            return;
        };
        if (!grid_roles.getSelected()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        var form=liger.get("searchBar2");
        var data = form.getData();
            data.structureID=tree.getSelected().data.id;
            data.roleID=grid_roles.getSelected().id;
        //alert(JSON.stringify(data));
        for (name in data) {grid_role_pe.setParm(name, data[name])};
        //设置默认一页大小
        grid_role_pe.setParm("pagesize", 1000);
        grid_role_pe.reload();
    });
    $("#addPE_btn").click(function(event) {
        if (!tree.getSelected()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        var selectedRoles=grid_roles.getSelecteds();
        if (selectedRoles.length==0) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        if (selectedRoles.length>1) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE_ONLY,SQ_type: "warning"});
            return;
        };
        $.ligerDialog.SQopen({
            width: 620,
            top: 25,
            title : '新增用户',
            target: $("#addPE_Html")
        });
        $("#searchBar_addPE").ligerForm();
        clearLigerForm("searchBar_addPE");
        var parms={
            "orgId": tree.getSelected().data.id,
            "roleId": grid_roles.getSelected().id,
            "usrNmSearch"  : ""
        };
        if (grid_addPE) {
            for (name in parms) {grid_addPE.setParm(name, parms[name])};
            grid_addPE.reload();
        }else{
            render_ligergrid_addPE (parms);
        };
    });
    $("#searchBtn_addPE").click(function(event) {
        var form=liger.get("searchBar_addPE");
        var data = form.getData();
            data.structureID=tree.getSelected().data.id;
            data.roleID=grid_roles.getSelected().id;
        //alert(JSON.stringify(data));
        for (name in data) {grid_addPE.setParm(name, data[name])};
        grid_addPE.reload();
    });
    $("#deletePE_btn").click(function(event) {
        if (!tree.getSelected()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
            return;
        };
        var selectedRoles=grid_roles.getSelecteds();
        if (selectedRoles.length==0) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE,SQ_type: "warning"});
            return;
        };
        if (selectedRoles.length>1) {
        	top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ROLE_ONLY,SQ_type: "warning"});
            return;
        };
        var selectedRoles=grid_role_pe.getSelecteds();
        if (selectedRoles.length==0) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_USR,SQ_type: "warning"});
            return;
        };
        var usrIdArr=[];
        orgId=tree.getSelected().data.id;
        roleId=grid_roles.getSelected().id;
        for (var i = 0; i < selectedRoles.length; i++) {
        	usrIdArr.push(selectedRoles[i].id);
        };
        $.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
			if(flag == false)return;
	        $.ajax({
	  			type: 'post',
	  			dataType:'json',
	  			data:{orgId:orgId,roleId:roleId,usrIdArr:usrIdArr},
	  			url: '<%=path%>/system/sysUsrFunAuthR/removeUsrByOrgIdRoleId.do',
	  			success:function(msg){
	  				 if(msg.success == 1){
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
	  					grid_role_pe.reload();
	  				 }else{
	  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
	  				 }
	  			}
	  		});
        });
        
    });
    $("#addPE_Html .l-dialog-btn-ok").click(function() {
        var selectedPE=grid_addPE.getSelecteds();
        if (selectedPE.length==0) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_USR,SQ_type: "warning"});
            return;
        };
        var usrIdArr=[];
        orgId=tree.getSelected().data.id;
        roleId=grid_roles.getSelected().id;
        for (var i = 0; i < selectedPE.length; i++) {
        	usrIdArr.push(selectedPE[i].id);
        };
        //alert(JSON.stringify(usrIdArr)+""+JSON.stringify(orgId)+""+JSON.stringify(roleId));
        $.ajax({
  			type: 'post',
  			dataType:'json',
  			data:{orgId:orgId,roleId:roleId,usrIdArr:usrIdArr},
  			url: '<%=path%>/system/sysUsrFunAuthR/addOrgRoleUsr.do',
  			success:function(msg){
  				 if(msg.success == 1){
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
  					grid_role_pe.reload();
  				 }else{
  					top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
  				 }
  			}
  		});
        $.ligerDialog.hide();
        top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_ADD_USR_SUCC,SQ_type: "success"});
    });
});
function set_ligergrid_roles (id) {
    var parms={"orgId": id};
    if (grid_roles) {
        grid_roles.grid.show();
        grid_roles.setParm("orgId", id);
        grid_roles.reload();
    }else{
        render_ligergrid_roles (parms)
    };
}
function render_ligergrid_roles (parms) {
    grid_roles = $("#ligergrid_roles").ligerGrid({
    	url: '<%=path%>/system/sysRole/getRoleByOrgId.do',
        parms: parms,
        method : 'get',
        root:'rows',
        record:'total',
        pageSize:10,
        fixedCellHeight: false,
        checkbox: true,
        selectRowButtonOnly: true,
        // isSingleCheck: true,
        frozenCheckbox: false,
        allowHideColumn: false,
        rownumbers:true,
        columns: [{
            display: 'ID',
            name: 'id',
            width: 80,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        },{
            display: '角色名称',
            name: 'roleNm',
            width: 120,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }, {
            display: '角色类别',
            name: 'roleTypCdNm',
            minWidth: 60,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }, {
            display: '状态',
            name: 'isValidNm',
            width: 60
        }, {
            display: '备注',
            name: 'roleRem',
            minWidth: 150,
            render: function (rowdata, rowindex, value){
        		return UIUtils.showColumnTip(value);
            }
        }],
        width: '100%',
        height:'100%',
        heightDiff:-2,
        hideLoadButton : true,
        // rownumbers: true,
        //usePagerOnLessPagesize: false,
        onSelectRow: function (rowdata, rowid, rowobj) {
            if (!tree.getSelected()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_ORG,SQ_type: "warning"});
                return;
            };
            //alert("角色id:"+rowdata.id+"\n"+"架构id:"+tree.getSelected().data.id);
            set_ligergrid_role_pe(tree.getSelected().data.id, rowdata.id);
        },
        // onCheckRow: function (checked,data,rowid,rowdata) {
        //     console.log(grid_roles.getSelecteds());
        // },
        onUnSelectRow: function (rowdata, rowid, rowobj) {
            grid_role_pe.grid.hide();
        },
        onAfterShowData: function() {
            if (grid_role_pe) {grid_role_pe.grid.hide()}
            SetIframe_height();
        }
    });
    grid_roles.setHeight(100);
    grid_roles.options.isSingleCheck=true;
}
// function detail_rolePE (that, role_id) {
//     if (!tree.getSelected()) {
//         ligerDialogWarn("请选择组织架构！", 1000);
//         return;
//     };
//     alert("角色id:"+role_id+"\n"+"架构id:"+tree.getSelected().data.id);
//     $(that).parents(".l-grid").find('.detail_rolePE_btn').removeClass('isSelected_btn');
//     $(that).addClass('isSelected_btn');
//     set_ligergrid_role_pe(tree.getSelected().data.id, role_id);
// }
function render_ligergrid_addRoles (parms) {
    grid_addRoles = $("#ligergrid_addRoles").ligerGrid({
    	url: '<%=path%>/system/sysRole/getRoleNotInOrgId.do',
        parms: parms,
        method : 'get',
        root:'rows',
        record:'total',
        fixedCellHeight: false,
        checkbox: true,
        selectRowButtonOnly: true,
        frozenCheckbox: false,
        allowHideColumn: false,
        rownumbers:true,
        columns: [{
            display: '角色名称',
            name: 'roleNm',
            width: 120
        },{
            display: '角色类别',
            name: 'roleTypCdNm',
            width: 120
        }, {
            display: '状态',
            name: 'isValidNm',
            width: 60,
            
        }, {
            display: '备注',
            name: 'roleRem',
            minWidth: 150,
        }],
        width: '100%'
    });
}
function set_ligergrid_role_pe (structure_id, role_id) {
    var parms={
        "orgId": structure_id,
        "roleId": role_id
    };
    if (grid_role_pe) {
        grid_role_pe.grid.show();
        // grid_role_pe.setParm("structureID", id);
        for (name in parms) {grid_role_pe.setParm(name, parms[name])};
        grid_role_pe.setParm("pagesize", 1000);
        grid_role_pe.reload();
    }else{
        render_ligergrid_role_pe (parms)
    };
}
function render_ligergrid_role_pe (parms) {
    grid_role_pe = $("#ligergrid_role_pe").ligerGrid({
    	url: '<%=path%>/system/sysUsr/getUsrByOrgIdRoleId.do',
        parms: parms,
        method : 'get',
        root:'rows',
        record:'total',
        checkbox: true,
        rownumbers:true,
        usePager: true,
        columns: [{
        	 display: '用户名',
             name: 'usrNm',
             minWidth: 100
        }, {
        	display: '真实名称',
            name: 'refRlNm',
            minWidth: 100
        }],
        width: '100%',
        height:'100%',
        pageSize:1000,
        fixedCellHeight: false,
        usePagerOnLessPagesize: false,
        heightDiff:-2
    });
    
    grid_role_pe.setHeight(100);
}
function render_ligergrid_addPE (parms) {
    grid_addPE = $("#ligergrid_addPE").ligerGrid({
    	url: '<%=path%>/system/sysUsr/getUsrNotInOrgIdRoleId.do',
        parms: parms,
        method : 'get',
        root:'rows',
        record:'total',
        rownumbers:true,
        //width: '300',
        columns: [{
        	display: '用户名',
            name: 'usrNm',
            minWidth: 100
        }, {
        	 display: '真实名称',
             name: 'refRlNm',
             minWidth: 100
        }],
        width: '100%',
        height:'400px',
        heightDiff:-2,
        pageSize:10
    });
    
    grid_addPE.setHeight(100);
}
function setNanoScroller () {
    $(".nanoWrap .nano").nanoScroller({
        preventPageScrolling: true
    });
}
</script>

</body>
</html>
